//
//  CZVistorView.swift
//  1215微博终极项目
//
//  Created by chok崋 on 15/12/16.
//  Copyright © 2015年 周卓华. All rights reserved.
//

import UIKit

//swift的代理
protocol CZVistorViewDelegate :NSObjectProtocol
{
       func vistorViewResigterBtn()
       func vistorLoginViewBtn()
}

class CZVistorView: UIView {

    //声明访客视图的代理
    weak var delegate:CZVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        
     //设置背景颜色
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //旋转转轮的动画方法
    func startRotationAnimation()
    {
         let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画相应的参数
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        //动画完成时不移除
        anim.removedOnCompletion = false
        //给图层添加到核心动画
        iconView.layer.addAnimation(anim, forKey: nil)
    
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复动画
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        // 重置偏移时间
        iconView.layer.timeOffset = 0
        
        // 重置开始时间
        iconView.layer.beginTime = 0
        
        // 计算开始时间
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        // 设置开始时间
        iconView.layer.beginTime = timeSincePause
    }

   

   private func prepareUI()
    {
        //添加到父容器中
        addSubview(iconView)
        
        //添加遮罩层
        addSubview(coverView)
        
        addSubview(homeView)
        addSubview(messageLbl)
        addSubview(loginBtn)
        addSubview(resigterBtn)
        
        //为了添加约束，而清楚掉那自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLbl.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        resigterBtn.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false;
        
        //iconView的x轴坐标
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //iconView的y轴坐标
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //房子参照转轮
        
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        self.addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
       
        //消息label
        self.addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        self.addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        
        
        //注册按钮
        self.addConstraint( NSLayoutConstraint(item: resigterBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
         self.addConstraint( NSLayoutConstraint(item: resigterBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
         self.addConstraint( NSLayoutConstraint(item: resigterBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
         self.addConstraint( NSLayoutConstraint(item: resigterBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
       
        //登录按钮
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        //遮罩层的约束
        self.addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
       
        self.addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        self.addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        self.addConstraint( NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: resigterBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
       
        
        
    }
    
    //进行切换访客视图的方法
    func setUpVistorInfo(imageName:String,title:String)
    {
        //图片
         iconView.image = UIImage(named: imageName)
        messageLbl.text = title
        //隐藏小屋子
        homeView.hidden = true
        //隐藏遮罩层
        coverView.hidden = true
    }
    
    //懒加载
    //注册按钮
  private  lazy var resigterBtn:UIButton =
    {
        let resigter = UIButton()
        //设置图片
        resigter.setBackgroundImage(UIImage(named:"common_button_white_disable" ), forState: UIControlState.Normal)
        //设置字体
        resigter.setTitle("注册", forState: UIControlState.Normal)
        resigter.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //添加按钮的事件
        resigter.addTarget(self, action: "resigterClick", forControlEvents:UIControlEvents.TouchUpInside)
        return resigter
        
    }()
    //回调注册按钮
    func resigterClick()
    {
         delegate?.vistorViewResigterBtn()
    }
    
    //登录按钮
   private lazy var loginBtn:UIButton = {
        
        let loginBtn = UIButton()
        loginBtn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    //添加按钮的事件
    loginBtn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return loginBtn
    }()
    
    
    func loginClick()
    {
         delegate?.vistorLoginViewBtn()
    }
    
    //轮子图片
  private  lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //家图片
   private lazy  var homeView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //消息
    private lazy var messageLbl:UILabel = {
        let message = UILabel()
        message.text = "关注这些人看看有什么惊喜"
        message.textColor = UIColor.darkGrayColor()
        //显示多行
        message.numberOfLines = 0
        return message
    }()
    
    //遮罩图层
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    
    
    
    
    
}
